import java.io.*;
import java.util.*;

/**
 * Created by Lenovo on 2017/6/1.
 */
public class ExpressionGenerator {
    public static void main(String[] args) throws IOException {
        Sort sort = new Sort();
        List<String> list = new ArrayList<String>();
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        Conversion con = new Conversion();
        Evaluation eva = new Evaluation();
        PrimaryTitle title = new PrimaryTitle();
        String str1 = args[0];
        String str2 = args[1];
        String str3 = args[2];
        String l1 = null, result2 = null, result1, result3 = null, result4 = null;
        int i = 0;
        if (args.length == 0) {
            System.out.println("No command line arguments");
        } else {
            FileOutputStream out = new FileOutputStream("./" + str3);
            i = Integer.parseInt(str2);
            for (int s = 0; s < Integer.parseInt(str1); s++) {
                int x = 0;
                Random random = new Random();
                int q = random.nextInt(2) + 1;
                if (str2.equals("1")) {
                    q = 3;
                }
                if (q == 1) {
                    l1 = title.promber3(i + 1, 9, 4, 1);//生成题目
                    result1 = con.storage(l1);//中转后
                    result3 = eva.calculate(result1);//计算当前
                    result2 = sort.sorting(result1);//排序
                    if (list.size() != 0) {
                        for (int w = 0; w < list.size(); w++) {
                            if (result2.equals(list.get(w))) {
                                result4 = eva.calculate(list2.get(w));
                                if (result3.equals(result4)) {
                                    x = 1;
                                    s --;
                                    break;
                                }
                            }
                        }
                    }
                    if (x == 0) {
                        list.add(result2);//排序
                        list1.add(l1);//原题目
                        list2.add(result1);//中转后
                    }
                } else if (q == 2) {
                    String l2 = title.promber1(i + 1, 9, 4, 1);
                    result1 = con.storage(l2);//中转后
                    result2 = sort.sorting(result1);//排序
                    result3 = eva.calculate(result1);//计算
                    if (list.size() != 0) {
                        for (int w = 0; w < list1.size(); w++) {
                            if (result2.equals(list.get(w))) {
                                result4 = eva.calculate(list2.get(w));
                                if (result3.equals(result4))
                                    x = 1;
                                s--;
                                break;
                            }
                        }
                    }
                    if (x == 0) {
                        list.add(result2);//排序
                        list1.add(l2);//原题目
                        list2.add(result1);//中转后
                    }
                } else if (q == 3) {
                    String l2 = title.promber1(i + 1, 9, 4, 1);
                    result1 = con.storage(l2);//中转后
                    result2 = sort.sorting(result1);//排序
                    if (list.contains(result2)) {
                            s --;
                        }
                    else{
                        list.add(result2);//排序
                        list1.add(l2);//原题目
                    }
                }
            }
            String s = " ";
            for (int i1 = 0; i1 < list1.size(); i1++) {
                s += list1.get(i1) + "\r\n";
            }
            String t = s.trim();
            byte[] b = t.getBytes();
            out.write(b);
            out.close();
        }
    }
}
